<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of signup
 *
 * @author Administrator
 */
class signup extends CI_Controller {

  //put your code here
  public function __construct() {
    parent::__construct();
    set_userdata('end_user_current_url', current_url());
    if (!userdata('country_id') || !userdata('state_id') || !userdata('city_id')) {
      if (!userdata('end_user_user_id')) {
        redirect('welcome');
      } else {
        redirect('welcome/location');
      }
      return;
    }
  }

  public function index() {
    $this->load->view('user/signup', array('captcha' => generate_captcha(338),
        'countries' => $this->country_model->get_all(),
        'title_html' => $this->lang->line('label.front-end.title.signup')));
  }

  public function process_ajax() {
    $errors = array();
    if (empty($_REQUEST['display_name'])) {
      $errors[] = '["display_name", false, "Please enter full name"]';
    }
    if (empty($_REQUEST['email'])) {
      $errors[] = '["email", false, "Please enter email"]';
    } else {
      if (!valid_email($_REQUEST['email'])) {
        $errors[] = '["email", false, "Email is not available"]';
      } else if (!$this->base_lib->check_exist($_REQUEST['email'], 'email', null)) {
        $errors[] = '["email", false, "Email is was registered"]';
      }
    }
    if (empty($_REQUEST['password'])) {
      $errors[] = '["password", false, "Please enter password"]';
    } else if (empty($_REQUEST['confirm_pass']) || $_REQUEST['confirm_pass'] != $_REQUEST['password']) {
      $errors[] = '["password", false, "Please confirm password"]';
    }
    if (!empty($errors)) {
      echo '[' . implode(',', $errors) . ']';
      return;
    }
    $user = $_REQUEST;
    $group = $this->group_model->get(array('name' => 'End User'));
    if (!empty($group)) {
      $user['group_id'] = $group['id'];
    } else
      $user['group_id'] = 0;
    $user['username'] = $_REQUEST['email'];
    $user = $this->user_model->create_obj($user);
    $end_user = $this->end_user_model->create_obj($_REQUEST);
    $errors = array_merge($this->user_model->validate($user), $this->end_user_model->validate($end_user));
    if (!empty($errors))
      echo '[["email", false, "We have some problems. Please try again"]]';
    else {
      $user['salt'] = $this->auth->generateActiveCode(10);
      $user['password'] = $user['confirm_pass'] = md5($user['salt'] . md5($user['password']));
      if ($this->setting_model->item('enviroment') == EVR_TEST) {
        $user['is_active'] = true;
        $user['active_code'] = '';
      } else {
        $user['is_active'] = false;
        $user['active_code'] = $this->auth->generateActiveCode(50) . uniqid();
      }
      $user['created_on'] = time();
      if (!empty($user['first_name']) || !empty($user['last_name'])) {
        $user['display_name'] = trim($user['first_name'] . ' ' . $user['last_name']);
      } else {
        $user['display_name'] = $user['username'];
      }
      $user_id = $this->user_model->save($user);
      if (is_integer($user_id)) {
        $avatar = $this->base_lib->_upload_img('avatar', 'end_user');
        $end_user['avatar'] = $avatar;
        $end_user['user_id'] = $user_id;
        $end_user['date_create'] = time();
        if ($this->setting_model->item('enviroment') == 1) {
          $end_user['status'] = PROFILE_STATUS_ACTIVE;
        } else {
          $end_user['status'] = PROFILE_STATUS_UNACTICE;
        }
        $end_user_id = $this->end_user_model->save($end_user);
        if (is_integer($end_user_id)) {
          if ($this->setting_model->item('require_active_via_email') && $user['is_active'] == false) {
            $this->load->library('end_user_lib');
            $this->end_user_lib->email_active_account(array_merge($user, $end_user, array('link' => site_url('profile/active/' . $user['active_code']))));
          }
          echo '[]';
          return;
        } else {
          $this->user_model->delete($user_id);
          echo '[["email", false, "We have some problems. Please try again"]]';
        }
      } else
        echo '[["email", false, "We have some problems. Please try again"]]';
    }
  }

  public function process() {
    $user = $_REQUEST;
    $group = $this->group_model->get(array('name' => 'End User'));
    if (!empty($group)) {
      $user['group_id'] = $group['id'];
    } else
      $user['group_id'] = 0;
    $user = $this->user_model->create_obj($user);
    $end_user = $this->end_user_model->create_obj($_REQUEST);
    $errors = array_merge($this->user_model->validate($user), $this->end_user_model->validate($end_user));
    if (!empty($errors))
      set_flash_error($errors);
    else {
      $user['salt'] = $this->auth->generateActiveCode(10);
      $user['password'] = $user['confirm_pass'] = md5($user['salt'] . md5($user['password']));
      if ($this->setting_model->item('enviroment') == EVR_TEST) {
        $user['is_active'] = true;
        $user['active_code'] = '';
      } else {
        $user['is_active'] = false;
        $user['active_code'] = $this->auth->generateActiveCode(50) . uniqid();
      }
      $user['created_on'] = time();
      if (empty($user['display_name'])) {
        if (!empty($user['first_name']) || !empty($user['last_name'])) {
          $user['display_name'] = trim($user['first_name'] . ' ' . $user['last_name']);
        } else {
          $user['display_name'] = $user['username'];
        }
      }
      $user_id = $this->user_model->save($user);
      if (is_integer($user_id)) {
        $avatar = $this->base_lib->_upload_img('avatar', 'end_user');
        $end_user['avatar'] = $avatar;
        $end_user['user_id'] = $user_id;
        $end_user['date_create'] = time();
        if ($this->setting_model->item('enviroment') == 1) {
          $end_user['status'] = PROFILE_STATUS_ACTIVE;
        } else {
          $end_user['status'] = PROFILE_STATUS_UNACTICE;
        }
        $end_user_id = $this->end_user_model->save($end_user);
        if (is_integer($end_user_id)) {
          if ($this->setting_model->item('require_active_via_email') && $user['is_active'] == false) {
            $this->load->library('end_user_lib');
            $this->end_user_lib->email_active_account(array_merge($user, $end_user, array('link' => site_url('profile/active/' . $user['active_code']))));
          }
          set_flash_message($this->lang->line('message.end_user.created', array($end_user_id)));
          redirect('');
          return;
        } else {
          $this->user_model->delete($user_id);
          set_flash_error($this->lang->line('error.faltal.error'));
        }
      } else
        set_flash_error($this->lang->line('error.faltal.error'));
    }
    $this->load->view('user/signup', array('obj' => $_REQUEST,
        'captcha' => generate_captcha(),
        'countries' => $this->country_model->get_all(),
        'title_html' => $this->lang->line('label.front-end.title.signup')));
  }

}